package starter.petstore.actions;

import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.restassured.http.ContentType;
import io.restassured.RestAssured;
import net.serenitybdd.core.steps.UIInteractions;
import org.hamcrest.Matchers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import static net.serenitybdd.rest.SerenityRest.*;

/**
 * @ClassName Actions
 * @Date 2024/11/11 14:03 45
 * @Author Jack Chen
 * @Description api-test-REST-Assured
 * @Version 1.0
 */

public class Actions extends UIInteractions {
    //private static Long petId = 0L;

    @When("user upload image")
    public void user_upload_image() {
        given().
                baseUri("https://petstore.swagger.io").
                basePath("/v2/pet").
                multiPart(new File("D:\\Myfiles\\pic\\tuzi.png"));
        when().
                post("/" + 9223372036854775807L +"/uploadImage");
    }

    @Then("user upload image successfully")
    public void userUploadImageSuccessfully() {
        then().
                //body("code", Matchers.equalTo(200),"message",Matchers.contains("./tuzi.png")).
                contentType(ContentType.JSON).
                log().all().
                statusCode(200).
                body("code", Matchers.equalTo(200)).
                body("message", Matchers.containsString("/tuzi.png"));

    }

    @When("user add a new pet to the store")
    public void userAddANewPetToTheStore() throws FileNotFoundException {
        String jsonFilePath = "src/test/java/starter/petstore/actions/pet.json";
        File file = new File(jsonFilePath);
        InputStream inputStream = new FileInputStream(file);
        given().
                baseUri("https://petstore.swagger.io").
                basePath("/v2/pet").
                contentType(ContentType.JSON).
                accept(ContentType.JSON).
                body(inputStream).
                post();
    }

    @Then("the add operation is successful")
    public void theAddOperationIsSuccessful() {
        then().log().all().statusCode(200).
                body("status", Matchers.containsString("available"));

    }

    @When("user update an existing pet")
    public void userUpdateAnExistingPet() throws FileNotFoundException {
        String jsonFilePath = "src/test/java/starter/petstore/actions/petUpdate.json";
        File file = new File(jsonFilePath);
        InputStream requestBody = new FileInputStream(file);
        given().
                baseUri("https://petstore.swagger.io").
                basePath("/v2/pet").
                contentType(ContentType.JSON).
                accept(ContentType.JSON).
                body(requestBody).
                //when().
                put();
    }

    @Then("the update operation is successful")
    public void theUpdateOperationIsSuccessful() {
        then().log().all().statusCode(200).
                body("name", Matchers.containsString("miaomi123"));
    }
}
